package com.item.news.runnable;

import org.springframework.util.ResourceUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Video_ScreenshotsRunnable
 * @Description TODO
 * @Author WangDong
 * @Date 2019/1/21 15:31
 * @Version 1.0
 **/
public class Video_ScreenshotsRunnable implements Runnable {

    String video_url;
    String img_url;
    //String startTime;

    public Video_ScreenshotsRunnable(String video_url, String img_url) {
        this.video_url = video_url;
        this.img_url = img_url;
    }

    @Override
    public void run() {
        List<String> commands = new ArrayList<String>();

        try {commands.add("C:/news/ffmpeg.exe");
        commands.add("-i");
        commands.add(video_url);
        commands.add("-y");
        commands.add("-f");
        commands.add("image2");
        commands.add("-ss");
        commands.add("1");// 这个参数是设置截取视频多少秒时的画面
        // commands.add("-t");
        // commands.add("0.001");
        commands.add("-s");
        String base64url="";

            commands.add("720x1280");
            String url="C:/image/";
            // judeDirExists(new File(url));

            File file = new File(url);
            if(!file.exists()){
                file.mkdirs();
            }

            commands.add(url+img_url);

            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commands);
            builder.start();
            System.out.println("截取成功");


        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    /*@Override
    public void run() {
        //String command = "ffmpeg -i " + video_url+ " -y -f image2 -ss "+startTime+" -t 00:00:01 -s  720x1280 " + img_url;
        String command = "ffmpeg -i"+video_url+" -ss"+ startTime+" -vframes 1 -r 1 -ac 1 -ab 2 -s 720*1280 -f  image2"+ img_url;
                       *//* ffmpeg -i $1 -ss $2 -vframes 1 -r 1 -ac 1 -ab 2 -s 320*180 -f  image2 $3*//*

        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(command);
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null)
                System.out.println(line);
        } catch (Throwable t) {
            t.printStackTrace();

        }

    }*/
}
